<script setup>
import {inject, ref} from 'vue';
import useDesignerStore from '../../store/index';
const designerStore = useDesignerStore();
const menuList = ref([
  {name:'打开文件夹', icon:'fa fa-folder-open', type: 'OPEN_FOLDER'},
  {name:'保存', icon:'fa fa-save', type: 'SAVE'},
  {name: '放大', icon: 'fa fa-search-plus',type: 'zoom-in'},
  {name: '缩小', icon: 'fa fa-search-minus',type: 'zoom-out'},
  {name:'居中',icon: 'fa fa-arrows-alt',type: 'center'},
  {name: '重置', icon: 'fa fa-refresh',type:'reset'},
  {name: '下载', icon: 'fa fa-download',type: 'download'},
  {name: '打印', icon: 'fa fa-print',type: 'print'},
  {name: '全屏', icon: 'fa fa-arrows-alt',type: 'full-screen'},
  {name: '撤销', icon: 'fa fa-undo',type: 'undo'},
  {name: '重做', icon: 'fa fa-repeat',type:'redo'},
  {name: '复制', icon: 'fa fa-copy',type: 'copy'},
  {name: '粘贴', icon: 'fa fa-paste',type: 'paste'},
  {name: '删除', icon: 'fa fa-trash',type: 'delete'},
  {name: '属性', icon: 'fa fa-info-circle',type: 'property'},
  {name: '帮助', icon: 'fa fa-question-circle',type: 'help'},
]);
const gMap=inject('gMap');
const Revoke=inject('Revoke');

const onMenuItemClick =async (type) => {
  switch (type) {
    case 'OPEN_FOLDER':
      let imageList=await window.electronApi.getDirectoryImages();
      console.log(imageList)
      designerStore.imageList=imageList;
      break;
    case 'SAVE':
      gMap.value.save();
      break;
    case 'zoom-in':
      gMap.value.zoomIn();
      break;
    case 'zoom-out':
      gMap.value.zoomOut();
      break;
      case 'center':
      gMap.value.setCenter(designerStore.center);
      break;
    case'reset':
      gMap.value.refresh();
      break;
    case 'download':
      gMap.value.exportLayersToImage(
          {x: 0, y: 0, width: 500, height: 354},
          {type: 'base64', format: 'image/png'}
      ).then(function(base64) {
          const link = document.createElement('a');
          link.download = 'image.png';
          link.href = base64;
          link.click();
      });
      break;
    case 'print':
      gMap.value.exportLayersToImage(
          {x: 0, y: 0, width: 500, height: 354},
          {type: 'blob', format: 'image/png'}
      ).then(function(blob) {
          let imgUrl = URL.createObjectURL(blob);
          let printWindow = window.open(imgUrl, 'printWindow', 'width=500,height=354');
          printWindow.document.write('<img src="' + imgUrl + '">');
          printWindow.document.close();
          printWindow.focus();
          printWindow.print();
          printWindow.close();
          URL.revokeObjectURL(imgUrl);
      });
      break;
    case 'full-screen':
      if (document.fullscreenElement) {
        document.exitFullscreen();
      } else {
        document.documentElement.requestFullscreen();
      }
      break;
    case 'undo':
      // Revoke();
        gMap.value.undo();
      break;
    case'redo':
      gMap.value.redo();
      break;
    case 'copy':
      gMap.value.copy();
      break;
    case 'paste':
      gMap.value.paste();
      break;
    case 'delete':
      gMap.value.delete();
      break;
    case 'property':
      gMap.value.showProperty();
      break;
    case 'help':
      gMap.value.showHelp();
      break;
    default:
      break;
  }
}
</script>
<template>
  <div class="top-menu-bar">
    <div class="menu">
      <div class="menu-item" v-for="(item, index) in menuList" :key="index" @click="onMenuItemClick(item.type)">
        <el-tooltip :content="item.name" placement="bottom">
          <i :class="item.icon"></i>
        </el-tooltip>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.top-menu-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 44px;
  background-color: #ffffff;
  color: #fff;
  padding: 0 20px;
  position: fixed;
  top: 10px;
  left: 50px;
  right: 0;
  width: 70%;
  margin: 0 auto;
  z-index: 9;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  border-radius: 10px;

  .menu {
    display: flex;
    width: 100%;
    overflow-y: hidden;
    overflow-x: scroll;
    transition: all 0.3s ease-in-out;
    -ms-overflow-style: none;  /* IE and Edge */
    scrollbar-width: none;  /* Firefox */
    &::-webkit-scrollbar {
      width: 0;  /* Remove scrollbar space */
      background: transparent;  /* Optional: just make scrollbar invisible */
    }

    .menu-item {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 50px;
      height: 50px;
      cursor: pointer;
      transition: background-color 0.3s ease;

      &:hover {
        background-color: #dcdcdc;
      }

      i {
        font-size: 22px;
        color: #0c0c0c;
      }
    }
  }
}


</style>